package com.cenwangkeji.resident.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.ServicePackageBean;
import com.cenwangkeji.resident.model.bean.User;
import com.cenwangkeji.resident.ui.adapter.ServicePackageAdpter;
import com.cenwangkeji.resident.ui.adapter.ServicePackageAllAdpter;
import com.cenwangkeji.resident.ui.adapter.ServicePackageFragmentAdpter;
import com.google.gson.Gson;
import com.lws.basic.base.BaseActivity;
import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.RxUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

//服务包全部
public class ServicePackageAllActivity extends BaseActivity {


    @BindView(R.id.rv)
    RecyclerView mRv;
    private ServicePackageAllAdpter mAdpter;

    @Override
    public int setLayoutId() {
        return R.layout.activity_service_package_all;
    }

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        mRv.setLayoutManager(new LinearLayoutManager(ServicePackageAllActivity.this));
        mAdpter = new ServicePackageAllAdpter(null);
        mRv.setAdapter(mAdpter);
        queryService();
    }

    //查询健康服务包
    public void queryService(){
        showLoading();
        Observable.create(new ObservableOnSubscribe<ServicePackageBean>() {
            @Override
            public void subscribe(ObservableEmitter<ServicePackageBean> emitter) throws Exception {
                Map<Object,Object> map = new HashMap<>();
                map.put("mark",3);
                map.put(Constant.token, User.getToken());
                map.put("userId", User.getUserId());
                JSONObject jsonObject = new JSONObject(map);
                String json = OkHttpUtils.post(jsonObject.toString(), Constant.service_package_user);
                MyLogger.i(json);
                Gson gson = new Gson();
                emitter.onNext(gson.fromJson(json, ServicePackageBean.class ));
            }
        }).compose(RxUtils.<ServicePackageBean>io_main())
                .subscribe(new ObserverOnNext<ServicePackageBean>() {



                    @Override
                    public void onNext(ServicePackageBean bean) {
                        if (bean.getData()!=null && bean.getData().size()>0){
                            mAdpter.setNewData(bean.getData());
                        }else {
                            MyToast.show(bean.getMsg());
                            View emptyView=getLayoutInflater().inflate(R.layout.view_empty, null);
                            mAdpter.setEmptyView(emptyView);
                        }
                        dismissLoading();
                    }
                });

    }



}
